package br.sales.view.bean;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.sales.controller.IUserEntityController;
import br.sales.controller.UserEntityControllerImpl;
import br.sales.entity.UserEntity;
import br.sales.entity.exception.EntityExistsException;
import br.sales.persistence.exception.DBContainerNotInitializedException;
import br.sales.view.util.ViewUtil;


@SessionScoped
@ManagedBean(name="mainBean")
public class MainBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private static final String CREATE_ACCOUNT = "CREATE_ACCOUNT";
	private static final String LOGIN = "LOGIN";
	
	private UserEntity loggedUser;
	private UserEntity userToLogin;
	private String currentPage;
	private String settingsPage;
	private String currentState = LOGIN;

	private IUserEntityController userEntityController;
	
	public MainBean() {
		System.out.println("CRIANDO MAINBEAN");
		if (currentPage==null || currentPage.isEmpty()){
			currentPage = "news.xhtml";
		}
		currentState = LOGIN;
		userEntityController  = UserEntityControllerImpl.getInstance();
	}
	
	
	public void prepareToCreateAccount(){
		setCurrentState(CREATE_ACCOUNT);
		userToLogin = new UserEntity();
	}
	
	public void cancelAccountCreate(){
		setCurrentState(LOGIN);
	}
	
	public String createAccount(){
		try {
			userToLogin.setEmail(userToLogin.getLogin());
			String pass = userToLogin.getPass();
			userToLogin.setType(UserEntity.COMMON);
			userEntityController.createUserEntity(userToLogin);
			userToLogin = new UserEntity(userToLogin.getLogin(),pass);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"account_created_success"), FacesMessage.SEVERITY_INFO);
			setCurrentState(LOGIN);
			return "home?faces-redirect=true";
		}catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return null;
		} catch (EntityExistsException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),"form_createAccount:inputText_create_login",ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"login_in_use"), FacesMessage.SEVERITY_ERROR);
			return null;
		}
	}
	
	public String login(){
		try {
			loggedUser = userEntityController.authenticate(userToLogin.getLogin(), userToLogin.getPass());
			
			if (loggedUser == null){
				ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"invalid_login"), FacesMessage.SEVERITY_ERROR);
				return null;
			} else {
				if (loggedUser.isActive()){
					ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"login_success"), FacesMessage.SEVERITY_INFO);
					return "settings?faces-redirect=true";
				} else {
					loggedUser = null;
					ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_not_active"), FacesMessage.SEVERITY_ERROR);
					return null;
				}
			}
		} catch (DBContainerNotInitializedException e) {
			e.printStackTrace();
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return null;
		}
	}
	
	public String logout(){
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.invalidate();
		this.loggedUser = null;
		this.setCurrentPage("news.xhtml");
		return "home?faces-redirect=true";
	}
	
	public String getSettingsPage() {
		return settingsPage;
	}

	public void setSettingsPage(String settingsPage) {
		this.settingsPage = settingsPage;
	}

	public void prepareLogin(){
		setCurrentState(LOGIN);
		userToLogin = new UserEntity();
	}
	
	public UserEntity getLoggedUser() {
		return loggedUser;
	}

	public void setLoggedUser(UserEntity loggedUser) {
		this.loggedUser = loggedUser;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public UserEntity getUserToLogin() {
		return userToLogin;
	}

	public void setUserToLogin(UserEntity userToLogin) {
		this.userToLogin = userToLogin;
	}
	
	public String getCurrentState() {
		return currentState;
	}

	public void setCurrentState(String currentState) {
		this.currentState = currentState;
	}

	public boolean isLoginState(){
		return currentState != null && currentState.equals(LOGIN);
	}
	
	public boolean isCreateAccountState(){
		return currentState != null && currentState.equals(CREATE_ACCOUNT);
	}
}
